package com.shirley.pers.zhufengfmvi.client;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.shirley.pers.zhufengfmvi.utils.HttpUtils;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: 工具类，内部就是所有需要调用的接口网络请求，每一个接口，实现一个方法，便于统一管理网址，参数和内容的解析。
 * 对于异步任务，只需要调用方法就好了，不需要考虑网址问题；<br/>
 * <p/>
 * Copyright (c) , 2016, Shirley Xian <br/>
 * This program is protected by copyright laws. <br/>
 * ClassName: ClientAPI<br/>
 * Date: 2016/4/6 11:27
 *
 * @author Shirley Xian
 * @version 1.0
 */
public final class ClientAPI {
    //通常 软件上线之前，都是使用测试服务器的。
    //上线的时候，就需要使用正式服务器的地址。
    public static final String API_POINT = "http://mobile.ximalaya.com/mobile";

    private ClientAPI() {
    }

    /**
     * 获取发现部分的推荐内容
     * 对于接口：11
     *
     * @return
     */
    public static String getDiscoveryRecommends(boolean includeActivity, boolean includeSpecial) {
        String ret = null;
        //1:拼网址
        String url = API_POINT + "/discovery/v1/recommends" + "?channel=and-f6" + "&device=android" + "&includeActivity=" + includeActivity + "&includeSpecial=" + includeSpecial + "&scale=2" + "&version=4.1.7.1";
        //2：请求
        byte[] bytes = HttpUtils.doGet(url);
        //3：返回结果
        if (bytes != null) {
            try {
                ret = new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 获取发现部分的分类
     *
     * @return
     */
    public static String getDiscoveryCategorys() {
        String ret = null;
        StringBuilder builder = new StringBuilder(API_POINT);
        builder.append("/discovery/v1/categories")
                .append("?device=android")
                .append("&picVersion=10")
                .append("&scale=2");

        String url = builder.toString();
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                ret = new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 获取专辑详情信息，专辑 203355 中 第一页共 20条信息的播放列表信息，推荐中-》专辑推荐图标点击进入专辑详情界面，调用接口17，主要返回 专辑基本信息，和专辑曲目列表
     * http://mobile.ximalaya.com/mobile/others/ca/album/track/203355/true/1/20?device=android&pageSize=20&albumId=203355&isAsc=true
     *
     * @param pageNum  第几页 页编号
     * @param pageSize 每页多少个条目
     * @param albumId  专辑id
     * @param isAsc    是否升序排列
     * @return
     */
    public static String getAlbumsDetail(@NonNull long pageNum, @NonNull long pageSize, @NonNull long albumId, @Nullable boolean isAsc) {
        String ret = null;
        StringBuilder builder = new StringBuilder(API_POINT);
//        http://mobile.ximalaya.com/mobile
        builder.append("/others/ca/album/track/").append(albumId).append("/").append(isAsc).append("/").append(pageNum).append("/").append(pageSize)
                .append("?device=android")
                .append("&pageSize=").append(pageSize)
                .append("&albumId=").append(albumId)
                .append("&isAsc=").append(isAsc);
        String url = builder.toString();
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                ret = new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    /**
     * http://mobile.ximalaya.com/mobile/track/detail?device=android&trackId=8060450
     * 获取曲目详情
     *
     * @param trackId 曲目id
     * @return
     */
    public static String getTrackDetailById(long trackId) {
        String ret = null;
        StringBuilder builder = new StringBuilder(API_POINT);
        builder.append("/track/detail?device=android&trackId=").append(trackId);
        String url = builder.toString();
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                ret = new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }
}
